﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Net;
using System.Text;
using System.Web;

namespace lxwmvc
{
    class core_user
    {
        /// <summary>
        /// 判断当前用户用户是否开户
        /// </summary>
        /// <param name="user_id">如果不填写，就代表当前用户，如果填写，就是指定用户</param>
        /// <returns></returns>
        public static bool checkUserSFKaiHu(string user_id = "-1")
        {
            if(user_id == "-1")
                user_id = userID;

            return supermodel
                .create("sysuser_kuozhan")
                .select("COUNT(user_id)")
                .where("UsrId is not null AND user_id='" + user_id + "'")
                .one<int>() > 0;
        }

        /// <summary>
        /// 获取用户的基本信息
        /// </summary>
        /// <param name="user_id"></param>
        /// <returns></returns>
        public static supermodel getUserInfo(string user_id = "-1")
        {
            if (user_id == "-1")
                user_id = userID;

            //sysuser.USER_ID,email,sysuser.user_id,MOBIL_NO,CardNum,sysuser_kuozhan.TrueName
            return supermodel
                .create("sysuser_kuozhan")
                .select("sysuser.UsrCustId,email,sysuser.user_id,MOBIL_NO,CardNum,sysuser_kuozhan.TrueName")
                .inner("sysuser_kuozhan", "sysuser.user_id=sysuser_kuozhan.user_id")
                .where("sysuser.user_id='" + user_id + "'")
                .get();
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="UsrCustId"></param>
        /// <returns></returns>
        public static supermodel getUserInfoByUsrCustId(string UsrCustId)
        {            

            //sysuser.USER_ID,email,sysuser.user_id,MOBIL_NO,CardNum,sysuser_kuozhan.TrueName
            return supermodel
                .create("sysuser_kuozhan")
                .select("email,sysuser.user_id,MOBIL_NO,CardNum,sysuser_kuozhan.TrueName")
                .inner("sysuser_kuozhan", "sysuser.user_id=sysuser_kuozhan.user_id")
                .where("sysuser.UsrCustId='" + UsrCustId + "'")
                .get();
        }

        /// <summary>
        /// 获取用户的汇付开户编号
        /// </summary>
        /// <param name="user_id"></param>
        /// <returns></returns>
        public static string getUsrCustId(string user_id = "-1")
        {
            //select UsrCustId from SysUser_kuozhan where user_id='" + userid + "'"
            if (user_id == "-1")
                user_id = userID;

            //sysuser.USER_ID,email,sysuser.user_id,MOBIL_NO,CardNum,sysuser_kuozhan.TrueName
            return supermodel
                .create("sysuser_kuozhan")
                .select("UsrCustId")
                .where("user_id='" + user_id + "'")
                .one<string>();
        }

        /// <summary>
        /// 根据用户开户号得到用户的ID
        /// </summary>
        /// <param name="UsrCustId"></param>
        /// <returns></returns>
        public static string getUserIdByUsrCustId(string UsrCustId = "-1")
        {
            return supermodel
                .create("sysuser_kuozhan")
                .select("user_id")
                .where("UsrCustId='" + UsrCustId + "'")
                .one<string>();
        }

        /// <summary>
        /// 获取用户的手机号码
        /// </summary>
        /// <param name="UsrCustId"></param>
        /// <returns></returns>
        public static string getMobileByUsrCustId(string UsrCustId = "-1")
        {
            return supermodel
                .create("sysuser_kuozhan")
                .select("MOBIL_NO")
                .where("UsrCustId='" + UsrCustId + "'")
                .one<string>();
        }

        /// <summary>
        /// 获取手机号
        /// </summary>
        /// <param name="UsrCustId"></param>
        /// <returns></returns>
        public static string getMobileByUserId(string UsrCustId = "-1")
        {
            return supermodel
                .create("sysuser_kuozhan")
                .select("MOBIL_NO")
                .where("user_id='" + UsrCustId + "'")
                .one<string>();
        }

        /// <summary>
        /// 设置用户是否登录
        /// </summary>
        public static string userID
        {
            get
            {
                return HttpContext.Current.Session["g_user_id"].tostr();
            }
            set
            {
                HttpContext.Current.Session["g_user_id"] = value;
            }
        }

        /// <summary>
        /// 给用户发送短信
        /// </summary>
        /// <param name="M_ToU_ID">接收人</param>
        /// <param name="M_Content">内容</param>
        public static void sendMessage(string M_ToU_ID, string M_Content)
        {
            supermodel model = supermodel.create("Message");
            model["M_Type"] = "4";
            model["M_FromU_Name"] = "系统管理员";
            model["M_ToU_ID"] = M_ToU_ID;
            model["M_Content"] = M_Content;
            model["M_Posttime"] = DateTime.Now.tostr();
            model["M_IsSend"] = "1";
            model["M_IsRead"] = "0";

            //插入一条记录到数据库
            model.saveorupdate();
        }

        /// <summary>
        /// 添加优惠与红包,这部分sql语句不优化了，写的真烂。。。。。。。。
        /// </summary>
        /// <param name="yhyhbid"></param>
        public static string addhongbao(string laiyuan)
        {            
            try
            {
                string sql = "";

                var user = supermodel.create("SysUser")
                    .select("DEPT_ID,USER_PRIV")
                    .where("user_id='"+core_user.userID+"'")
                    .get();

                if (user.Count == 0) throw new Exception("用户不存在！");

                sql = "select top 1 yhyhbid from pyhyhb where zhuangtai='正常' and (GETDATE() BETWEEN Youxiaoqi AND Youxiaoqizhi) " + "and Shiyongbumen like '%{0},%' and Shiyongjuese like '%{1},%' and Laiyuan='{2}' order by chushenshijian desc";
                sql = string.Format(sql, user["DEPT_ID"], user["USER_PRIV"], laiyuan.Trim());
                var yhyhbid = queryhelper.get(sql);

                if(yhyhbid.toint32() == 0) return "没有找到优惠红包";

                sql = "select count(1) from user_youhui where user_id='" + core_user.userID  +"'";
                if (queryhelper.get(sql).toint32() > 0) return "您已经领取过该红包";

                sql = " insert into user_youhui (user_id,yhyhbid,jine,shijian,Duihuanfangshi,Laiyuan,Zhuangtai) select {0},yhyhbid,Jine,GETDATE(),Duihuanfangshi,Laiyuan,'正常' from  pyhyhb where yhyhbid={1}";

                sql = string.Format(sql, core_user.userID, yhyhbid);
                queryhelper.execute(sql);

                return "成功获取红包";
            }
            catch (Exception ex)
            {
                dataloger.fatal("添加优惠和红包发生错误", ex);
            }
            return "获取红包失败";
        }
    }
}
